
typedef unsigned char       BOOL;

typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

typedef unsigned char       UINT8;
typedef unsigned short      UINT16;
typedef unsigned long       UINT32;

typedef signed char         INT8;
typedef signed short        INT16;
typedef signed long         INT32;

void halSpiStrobe(BYTE strobe);

BYTE halSpiReadStatus(BYTE addr);

void halSpiWriteReg(BYTE addr, BYTE value);

BYTE halSpiReadReg(BYTE addr);

void halSpiWriteBurstReg(BYTE addr, BYTE *buffer, BYTE count);

void halSpiReadBurstReg(BYTE addr, BYTE *buffer, BYTE count);

typedef struct S_RF_SETTINGS{
BYTE FSCTRL1;   
BYTE FSCTRL0;   
BYTE FREQ2;     
BYTE FREQ1;     
BYTE FREQ0;     
BYTE MDMCFG4;   
BYTE MDMCFG3;   
BYTE MDMCFG2;   
BYTE MDMCFG1;   
BYTE MDMCFG0;   
BYTE CHANNR;    
BYTE DEVIATN;   
BYTE FREND1;    
BYTE FREND0;    
BYTE MCSM0;     
BYTE FOCCFG;    
BYTE BSCFG;     
BYTE AGCCTRL2;  
BYTE AGCCTRL1;  
BYTE AGCCTRL0;  
BYTE FSCAL3;    
BYTE FSCAL2;    
BYTE FSCAL1;    
BYTE FSCAL0;    
BYTE FSTEST;    
BYTE TEST2;     
BYTE TEST1;     
BYTE TEST0;     
BYTE IOCFG2;    
BYTE IOCFG0;    
BYTE PKTCTRL1;  
BYTE PKTCTRL0;  
BYTE ADDR;      
BYTE PKTLEN;    
} RF_SETTINGS;

void halRfWriteRfSettings(flash RF_SETTINGS *pRfSettings);

void halRfSendPacket(BYTE *txBuffer, UINT8 size);

#pragma used+
sfrb PINB=3;
sfrb DDRB=4;
sfrb PORTB=5;
sfrb PINC=6;
sfrb DDRC=7;
sfrb PORTC=8;
sfrb PIND=9;
sfrb DDRD=0xa;
sfrb PORTD=0xb;
sfrb TIFR0=0x15;
sfrb TIFR1=0x16;
sfrb TIFR2=0x17;
sfrb PCIFR=0x1b;
sfrb EIFR=0x1c;
sfrb EIMSK=0x1d;
sfrb GPIOR0=0x1e;
sfrb EECR=0x1f;
sfrb EEDR=0x20;
sfrb EEARL=0x21;
sfrb EEARH=0x22;
sfrw EEAR=0x21;   
sfrb GTCCR=0x23;
sfrb TCCR0A=0x24;
sfrb TCCR0B=0x25;
sfrb TCNT0=0x26;
sfrb OCR0A=0x27;
sfrb OCR0B=0x28;
sfrb GPIOR1=0x2a;
sfrb GPIOR2=0x2b;
sfrb SPCR=0x2c;
sfrb SPSR=0x2d;
sfrb SPDR=0x2e;
sfrb ACSR=0x30;
sfrb SMCR=0x33;
sfrb MCUSR=0x34;
sfrb MCUCR=0x35;
sfrb SPMCSR=0x37;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x01
	.EQU __sm_mask=0x0E
	.EQU __sm_adc_noise_red=0x02
	.EQU __sm_powerdown=0x04
	.EQU __sm_powersave=0x06
	.EQU __sm_standby=0x0C
	.SET power_ctrl_reg=smcr
	#endif
#endasm

#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-

extern unsigned char rf_err;

unsigned char SPI_WAIT()
{
unsigned char i;
for(i=0;i<200;i++)
{
if ((SPSR & (1<<7)))
return 1;
delay_us(1); 
}
return 0;
}

unsigned char GDO0_H_WAIT()
{
unsigned char i;
for(i=0;i<200;i++)
{
if ( !PIND.3 )
return 1;
delay_ms(1);
}
rf_err = 1;
return 0;
}

unsigned char GDO0_L_WAIT()
{
unsigned char i;
for(i=0;i<200;i++)
{
if ( PIND.3 )
return 1;
delay_ms(1);
}
rf_err = 1;
return 0;
}

unsigned char MISO_WAIT()
{
unsigned char i;
for(i=0;i<10;i++)
{
if ( !(PINB.4) )
return 1;
delay_ms(1);
}
return 0;
}

void halSpiWriteBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count) {
UINT8 i;
{PORTB &= ~(1<<2);};
MISO_WAIT() ;
SPDR = addr | 0x40;
SPI_WAIT();
SPDR = count;
SPI_WAIT();
for (i = 0; i < count; i++) {
SPDR = buffer[i];
SPI_WAIT();
}
{PORTB |= (1<<2);};
}
void halSpiWriteReg(unsigned char addr, unsigned char value) {
{PORTB &= ~(1<<2);};
MISO_WAIT();
SPDR = addr;
SPI_WAIT();
SPDR = value;
SPI_WAIT();
{PORTB |= (1<<2);};
}

flash RF_SETTINGS rfSettings = {
0x08,   
0x00,   
0x21,   
0x7B,   
0x13,   
0xCA,   
0x83,   
0x83,   
0x22,   
0xF8,   
0x00,   
0x34,   
0x56,   
0x10,   
0x18,   
0x16,   
0x6C,   
0x43,   
0x40,   
0x91,   
0xE9,   
0x2A,   
0x00,   
0x1F,   
0x59,   
0x81,   
0x35,   
0x09,   

0x29,   
0x06,   
0x04,   
0x05,   
0x00,   
62    
};
unsigned char paTable[] = {0x60};

void halRfWriteRfSettings(flash RF_SETTINGS *pRfSettings) {
halSpiWriteReg(0x0B        ,pRfSettings->FSCTRL1);  
halSpiWriteReg(0x0C        ,  pRfSettings->FSCTRL0);
halSpiWriteReg(0x0D        ,    pRfSettings->FREQ2);
halSpiWriteReg(0x0E        ,    pRfSettings->FREQ1);
halSpiWriteReg(0x0F        ,    pRfSettings->FREQ0);
halSpiWriteReg(0x10        ,  pRfSettings->MDMCFG4);
halSpiWriteReg(0x11        ,  pRfSettings->MDMCFG3);
halSpiWriteReg(0x12        ,  pRfSettings->MDMCFG2);
halSpiWriteReg(0x13        ,  pRfSettings->MDMCFG1);
halSpiWriteReg(0x14        ,  pRfSettings->MDMCFG0);
halSpiWriteReg(0x0A        ,   pRfSettings->CHANNR);
halSpiWriteReg(0x15        ,  pRfSettings->DEVIATN);
halSpiWriteReg(0x21        ,   pRfSettings->FREND1);
halSpiWriteReg(0x22        ,   pRfSettings->FREND0);
halSpiWriteReg(0x18         ,   pRfSettings->MCSM0 );
halSpiWriteReg(0x19        ,   pRfSettings->FOCCFG);
halSpiWriteReg(0x1A        ,    pRfSettings->BSCFG);
halSpiWriteReg(0x1B        , pRfSettings->AGCCTRL2);
halSpiWriteReg(0x1C        , pRfSettings->AGCCTRL1);
halSpiWriteReg(0x1D        , pRfSettings->AGCCTRL0);
halSpiWriteReg(0x23        ,   pRfSettings->FSCAL3);
halSpiWriteReg(0x24        ,   pRfSettings->FSCAL2);
halSpiWriteReg(0x25        ,   pRfSettings->FSCAL1);
halSpiWriteReg(0x26        ,   pRfSettings->FSCAL0);
halSpiWriteReg(0x29        ,   pRfSettings->FSTEST);
halSpiWriteReg(0x2C        ,    pRfSettings->TEST2);
halSpiWriteReg(0x2D        ,    pRfSettings->TEST1);
halSpiWriteReg(0x2E        ,    pRfSettings->TEST0);
halSpiWriteReg(0x00        ,   pRfSettings->IOCFG2);
halSpiWriteReg(0x02        ,   pRfSettings->IOCFG0);    
halSpiWriteReg(0x07        , pRfSettings->PKTCTRL1);
halSpiWriteReg(0x08        , pRfSettings->PKTCTRL0);
halSpiWriteReg(0x09        ,     pRfSettings->ADDR);
halSpiWriteReg(0x06        ,   pRfSettings->PKTLEN);

}
void halSpiReadBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count) {
UINT8 i;
{PORTB &= ~(1<<2);};
MISO_WAIT() ;
SPDR = (addr | 0xC0);
SPI_WAIT();
for (i = 0; i < count; i++) {
SPDR = 0;
SPI_WAIT();
buffer[i] = SPDR;
}
{PORTB |= (1<<2);};
}
unsigned char halSpiReadReg(unsigned char addr) {
UINT8 x;
{PORTB &= ~(1<<2);};
MISO_WAIT() ;
SPDR = (addr | 0x80);
SPI_WAIT();
SPDR = 0;
SPI_WAIT();
x = SPDR;
{PORTB |= (1<<2);};
return x;
}
unsigned char halSpiReadStatus(unsigned char addr) {
UINT8 x;
{PORTB &= ~(1<<2);};
MISO_WAIT() ;
SPDR = (addr | 0xC0);
SPI_WAIT();
SPDR = 0;
SPI_WAIT();
x = SPDR;
{PORTB |= (1<<2);};
return x;
}   
void halSpiStrobe(unsigned char strobe) {
{PORTB &= ~(1<<2);};
MISO_WAIT() ;
SPDR = strobe;
SPI_WAIT();
{PORTB |= (1<<2);};
}
void halRfSendPacket(unsigned char *txBuffer, UINT8 size) 
{   

delay_ms(10);

halSpiStrobe(0x36        );

halSpiWriteBurstReg(0x3F, txBuffer, size);
halSpiStrobe(0x35        );

GDO0_L_WAIT() ;

GDO0_H_WAIT() ;
}
BOOL RfReceivePacket(unsigned char *rxBuffer, UINT8 *length) {
unsigned char status[2];
UINT8 packetLength;
char tmp;

halSpiStrobe(0x34        );

GDO0_H_WAIT() ;
tmp = (halSpiReadStatus(0x3B) & 0x7F  );  
if (tmp)
{

packetLength = halSpiReadReg(0x3F); 

if (packetLength <= *length) {
halSpiReadBurstReg(0x3F, rxBuffer, packetLength); 
*length = packetLength;

halSpiReadBurstReg(0x3F, status, 2);

return (status[1] & 0x80);
} else {
*length = packetLength;

halSpiStrobe(0x3A        );
return 0;
}
} 
else return 0;

}

void Reset_CCxx00(void)
{
{PORTB |= (1<<2);};
delay_us(100);
{PORTB &= ~(1<<2);};
delay_ms(1);
{PORTB |= (1<<2);};
delay_ms(10);
{PORTB &= ~(1<<2);};
MISO_WAIT();
SPDR = 0x30        ;
SPI_WAIT();
MISO_WAIT();
{PORTB |= (1<<2);};
}
void cc1100_init (void)
{
Reset_CCxx00();
halRfWriteRfSettings(&rfSettings);
halSpiWriteBurstReg(0x3E, paTable, sizeof(paTable));
halSpiWriteReg(0x3e, 0xc3);  
halSpiWriteReg(0x3e, 0xc0);  
}
